﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace client.app.gui2
{
    /// <summary>
    /// ConsoleUI.xaml 的交互逻辑
    /// </summary>
    public partial class ConsoleUI : UserControl
    {
        public const int CONSOLE_MAX = 200;

        Dictionary<comm.ConsoleColor, Color> _colorDic = new Dictionary<comm.ConsoleColor, Color>();

        public ConsoleUI()
        {
            InitializeComponent();

            Console.OnWriteLine = ConsoleWriteLine;

            _colorDic.Add(comm.ConsoleColor.NORMAL, Color2.White(50));
            _colorDic.Add(comm.ConsoleColor.RED, Color2.Red());
            _colorDic.Add(comm.ConsoleColor.YELLOW, Color2.Yellow());
        }

        void ConsoleWriteLine(object obj, comm.ConsoleColor color, StackFrame stackFrame)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                var fontColor = _colorDic[color];
                var txt = obj == null ? "null" : obj.ToString();
                var time = DateTime.Now;
                var grid = new Grid();

                #region tbTxt
                var tbTxt = new TextBox
                {
                    IsReadOnly = true,
                    BorderThickness = new Thickness(0),
                    Background = null,
                    FontSize = 12,
                    TextWrapping = TextWrapping.Wrap,
                    Foreground = new SolidColorBrush(fontColor),
                    Text = txt,
                    Margin = new Thickness(20, 0, 0, 0)
                };
                grid.Children.Add(tbTxt);
                #endregion

                #region btnExpand
                var btnExpand = new Button1
                {
                    Text = "+",
                    Width = 18,
                    Height = 18,
                    Opacity = .5,
                    Foreground = new SolidColorBrush(Color2.White()),
                    VerticalAlignment = VerticalAlignment.Top,
                    HorizontalAlignment = HorizontalAlignment.Left
                };
                btnExpand.Click += new RoutedEventHandler((o, e) =>
                {
                    if (btnExpand.Text == "+")
                    {
                        tbTxt.Text = "Time:" + time + "\r\n" +
                            "File:" + stackFrame.GetFileName() + "\r\n" +
                            "Line:" + stackFrame.GetFileLineNumber() + "\r\n" +
                            "Column:" + stackFrame.GetFileColumnNumber() + "\r\n" +
                            "-----------------------\r\n" +
                             txt;
                        tbTxt.Foreground = new SolidColorBrush(Color2.White(30));
                        btnExpand.Text = "-";
                    }
                    else
                    {
                        tbTxt.Text = txt;
                        tbTxt.Foreground = new SolidColorBrush(fontColor);
                        btnExpand.Text = "+";
                    }
                });
                grid.Children.Add(btnExpand);
                #endregion

                spList.Children.Add(grid);

                if (spList.Children.Count > CONSOLE_MAX)
                {
                    spList.Children.RemoveAt(0);
                }

                svList.ScrollToBottom();

                this.UpBeginDraw();
            }));
        }
    }
}
